import {createRouter, createWebHistory} from "vue-router";
import { unauthorized} from "@/net/index.js";

const router  = createRouter({
    history: createWebHistory(import.meta.env.BASE_URL),
    routes:[
        {
            path: '/',
            name: 'welcome',
            component: () =>import('@/views/WelcomeView.vue'),
            children:[
                {
                    path: '',
                    name: 'welcome-login',
                    component:() => import('@/views/welcome/LoginPage.vue')
                },{
                    path: 'register',
                    name: 'welcome-register',
                    component:() => import('@/views/welcome/RegisterPage.vue')
                },{
                    path: 'reset',
                    name: 'welcome-reset',
                    component:() => import('@/views/welcome/ResetPage.vue')
                }
            ]
        }, {
            path: '/index',
            name: 'index',
            component: () => import('@/views/index/IndexView.vue'),
            redirect: '/account',
            // 二级路由
            children:[
                {
                    path: '/dataAnalysis',
                    name: 'dataAnalysis',
                    component: () => import('@/views/dataAnalysis/Data.vue')
                },
                {
                    path: '/account',
                    name: 'account',
                    component: () => import('@/views/account/Account.vue')
                },
                {
                    path: '/test1',
                    name: 'tes1',
                    component: () => import('@/views/test/Test1.vue')
                },
                {
                    path: '/tif',
                    name: 'tif',
                    component: () => import('@/views/tif/Tif.vue')
                },
                {
                    path: '/test2',
                    name: 'test2',
                    component: () => import('@/views/test/Test2.vue')
                }
            ]
        }
    ]
})

router.beforeEach((to, from, next) => {
    const isUnauthorized = unauthorized()
    if(to.name.startsWith('welcome-') && !isUnauthorized){
        next('/index')
    }else if(to.fullPath.startsWith('/index') && isUnauthorized){
        next('/')
    }else{
        next()
    }
})
export default router